package com.module.admin.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.app.dao.AppAntDao;
import com.module.admin.app.pojo.AppAnt;
import com.module.admin.app.service.AppAntService;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;

/**
 * app_ant的Service
 * @author admin
 * @date 2021-01-02 10:31:29
 * @version V1.0.0
 */
@Component
public class AppAntServiceImpl implements AppAntService {

	@Autowired
	private AppAntDao appAntDao;
	
	/**
	 * 保存或修改
	 * @param appAnt
	 * @return
	 */
	@Override
	public ResponseFrame saveOrUpdate(AppAnt appAnt) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(appAnt.getAaId())) {
			appAnt.setAaId(FrameNoUtil.uuid());
			if (FrameStringUtil.isEmpty(appAnt.getParentAaId())) {
				appAnt.setParentAaId("0");
			}
			appAntDao.save(appAnt);
		} else {
			appAntDao.update(appAnt);
		}
		frame.setSucc();
		return frame;
	}
	
	/**
	 * 根据aaId获取对象
	 * @param aaId
	 * @return
	 */
	@Override
	public AppAnt get(String aaId) {
		return handleDtl(appAntDao.get(aaId));
	}

	/**
	 * 处理字典等详细信息的方法
	 * @param appAnt
	 */
	private AppAnt handleDtl(AppAnt appAnt) {
		if (appAnt != null) {
		}
		return appAnt;
	}
	
	/**
	 * 根据aaId删除对象
	 * @param aaId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String aaId) {
		ResponseFrame frame = new ResponseFrame();
		appAntDao.delete(aaId);
		frame.setSucc();
		return frame;
	}

	@Override
	public List<AppAnt> findTree(AppAnt appAnt) {
		List<AppAnt> data = new ArrayList<AppAnt>();
		List<String> dealList = new ArrayList<>();
		List<AppAnt> allList = appAntDao.findAppAnt(appAnt);
		for (AppAnt ant : allList) {
			if ("0".equals(ant.getParentAaId())) {
				dealList.add(ant.getAaId());
				List<AppAnt> cldList = new ArrayList<>();
				for (AppAnt ant1 : allList) {
					if (ant.getAaId().equals(ant1.getParentAaId())) {
						dealList.add(ant1.getAaId());
						cldList.add(ant1);
						
						List<AppAnt> cld1List = new ArrayList<>();
						for (AppAnt ant2 : allList) {
							if (ant1.getAaId().equals(ant2.getParentAaId())) {
								dealList.add(ant2.getAaId());
								cld1List.add(ant2);
							}
						}
						ant1.setCldList(cld1List);
					}
				}
				ant.setCldList(cldList);
				data.add(ant);
			}
		}
		for (AppAnt ant : allList) {
			if (!dealList.contains(ant.getAaId())) {
				data.add(ant);
			}
		}
		return data;
	}
}